package c.c.b.a.b.a.g;

import c.c.b.a.a.k;
import c.c.b.a.a.o;
import c.c.b.a.a.r;
import c.c.b.a.a.v;
import c.c.b.a.a.w;
import c.c.b.a.a.x;
import c.c.b.a.b.a.e.h;
import c.c.b.a.b.a.e.j;
import c.c.b.a.b.c0;
import c.c.b.a.b.z;
import java.io.EOFException;
import java.io.IOException;
import java.net.ProtocolException;
import java.net.Proxy.Type;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

/* compiled from: Http1Codec */
public final class a implements c.c.b.a.b.a.e.d {
    public final z a;
    public final c.c.b.a.b.a.c.g b;
    public final c.c.b.a.a.g c;
    public final c.c.b.a.a.f d;
    public int e = 0;
    public long f = 262144;

    /* compiled from: Http1Codec */
    public abstract class b implements w {
        public final k a;
        public boolean b;
        public long c = 0;

        public b(a aVar) {
            this.a = new k(a.this.c.a());
        }

        public x a() {
            return this.a;
        }

        public long b(c.c.b.a.a.e eVar, long j) throws IOException {
            try {
                long b = a.this.c.b(eVar, j);
                if (b > 0) {
                    this.c += b;
                }
                return b;
            } catch (IOException e) {
                k(false, e);
                throw e;
            }
        }

        public final void k(boolean z, IOException iOException) throws IOException {
            a aVar = a.this;
            int i = aVar.e;
            if (i != 6) {
                if (i == 5) {
                    aVar.d(this.a);
                    a aVar2 = a.this;
                    aVar2.e = 6;
                    c.c.b.a.b.a.c.g gVar = aVar2.b;
                    if (gVar != null) {
                        gVar.f(z ^ 1, aVar2, this.c, iOException);
                    }
                    return;
                }
                StringBuilder g = c.b.a.a.a.g("state: ");
                g.append(a.this.e);
                throw new IllegalStateException(g.toString());
            }
        }
    }

    /* compiled from: Http1Codec */
    public final class c implements v {
        public final k a;
        public boolean b;

        public c() {
            this.a = new k(a.this.d.a());
        }

        public x a() {
            return this.a;
        }

        public synchronized void close() throws IOException {
            if (!this.b) {
                this.b = true;
                a.this.d.b("0\r\n\r\n");
                a.this.d(this.a);
                a.this.e = 3;
            }
        }

        public synchronized void flush() throws IOException {
            if (!this.b) {
                a.this.d.flush();
            }
        }

        public void g0(c.c.b.a.a.e eVar, long j) throws IOException {
            if (this.b) {
                throw new IllegalStateException("closed");
            } else if (j != 0) {
                a.this.d.j(j);
                String str = "\r\n";
                a.this.d.b(str);
                a.this.d.g0(eVar, j);
                a.this.d.b(str);
            }
        }
    }

    /* compiled from: Http1Codec */
    public final class e implements v {
        public final k a;
        public boolean b;
        public long c;

        public e(long j) {
            this.a = new k(a.this.d.a());
            this.c = j;
        }

        public x a() {
            return this.a;
        }

        public void close() throws IOException {
            if (!this.b) {
                this.b = true;
                if (this.c <= 0) {
                    a.this.d(this.a);
                    a.this.e = 3;
                    return;
                }
                throw new ProtocolException("unexpected end of stream");
            }
        }

        public void flush() throws IOException {
            if (!this.b) {
                a.this.d.flush();
            }
        }

        public void g0(c.c.b.a.a.e eVar, long j) throws IOException {
            if (this.b) {
                throw new IllegalStateException("closed");
            }
            c.c.b.a.b.a.e.m(eVar.b, 0, j);
            if (j <= this.c) {
                a.this.d.g0(eVar, j);
                this.c -= j;
                return;
            }
            StringBuilder g = c.b.a.a.a.g("expected ");
            g.append(this.c);
            g.append(" bytes but received ");
            g.append(j);
            throw new ProtocolException(g.toString());
        }
    }

    /* compiled from: Http1Codec */
    public class d extends b {
        public final c.c.b.a.b.w e;
        public long f = -1;
        public boolean g = true;

        public d(c.c.b.a.b.w wVar) {
            super(null);
            this.e = wVar;
        }

        public long b(c.c.b.a.a.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else if (!this.g) {
                return -1;
            } else {
                long j2 = this.f;
                if (j2 == 0 || j2 == -1) {
                    if (j2 != -1) {
                        a.this.c.p();
                    }
                    try {
                        this.f = a.this.c.m();
                        String trim = a.this.c.p().trim();
                        if (this.f < 0 || !(trim.isEmpty() || trim.startsWith(";"))) {
                            StringBuilder stringBuilder = new StringBuilder();
                            stringBuilder.append("expected chunk size and optional extensions but was \"");
                            stringBuilder.append(this.f);
                            stringBuilder.append(trim);
                            stringBuilder.append("\"");
                            throw new ProtocolException(stringBuilder.toString());
                        }
                        if (this.f == 0) {
                            this.g = false;
                            a aVar = a.this;
                            c.c.b.a.b.a.e.f.c(aVar.a.i, this.e, aVar.g());
                            k(true, null);
                        }
                        if (!this.g) {
                            return -1;
                        }
                    } catch (NumberFormatException e) {
                        throw new ProtocolException(e.getMessage());
                    }
                }
                long b = super.b(eVar, Math.min(j, this.f));
                if (b != -1) {
                    this.f -= b;
                    return b;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                k(false, protocolException);
                throw protocolException;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (this.g && !c.c.b.a.b.a.e.p(this, 100, TimeUnit.MILLISECONDS)) {
                    k(false, null);
                }
                this.b = true;
            }
        }
    }

    /* compiled from: Http1Codec */
    public class f extends b {
        public long e;

        public f(a aVar, long j) throws IOException {
            super(null);
            this.e = j;
            if (j == 0) {
                k(true, null);
            }
        }

        public long b(c.c.b.a.a.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else {
                long j2 = this.e;
                if (j2 == 0) {
                    return -1;
                }
                long b = super.b(eVar, Math.min(j2, j));
                if (b != -1) {
                    j2 = this.e - b;
                    this.e = j2;
                    if (j2 == 0) {
                        k(true, null);
                    }
                    return b;
                }
                ProtocolException protocolException = new ProtocolException("unexpected end of stream");
                k(false, protocolException);
                throw protocolException;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (!(this.e == 0 || c.c.b.a.b.a.e.p(this, 100, TimeUnit.MILLISECONDS))) {
                    k(false, null);
                }
                this.b = true;
            }
        }
    }

    /* compiled from: Http1Codec */
    public class g extends b {
        public boolean e;

        public g(a aVar) {
            super(null);
        }

        public long b(c.c.b.a.a.e eVar, long j) throws IOException {
            if (j < 0) {
                throw new IllegalArgumentException(c.b.a.a.a.B("byteCount < 0: ", j));
            } else if (this.b) {
                throw new IllegalStateException("closed");
            } else if (this.e) {
                return -1;
            } else {
                long b = super.b(eVar, j);
                if (b != -1) {
                    return b;
                }
                this.e = true;
                k(true, null);
                return -1;
            }
        }

        public void close() throws IOException {
            if (!this.b) {
                if (!this.e) {
                    k(false, null);
                }
                this.b = true;
            }
        }
    }

    public a(z zVar, c.c.b.a.b.a.c.g gVar, c.c.b.a.a.g gVar2, c.c.b.a.a.f fVar) {
        this.a = zVar;
        this.b = gVar;
        this.c = gVar2;
        this.d = fVar;
    }

    public void a(c0 c0Var) throws IOException {
        Type type = this.b.g().c.b.type();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(c0Var.b);
        stringBuilder.append(' ');
        Object obj = (c0Var.a.a.equals("https") || type != Type.HTTP) ? null : 1;
        if (obj != null) {
            stringBuilder.append(c0Var.a);
        } else {
            stringBuilder.append(b.i.b.f.i(c0Var.a));
        }
        stringBuilder.append(" HTTP/1.1");
        e(c0Var.c, stringBuilder.toString());
    }

    public c.c.b.a.b.e b(c.c.b.a.b.c cVar) throws IOException {
        Objects.requireNonNull(this.b.f);
        String c = cVar.f.c("Content-Type");
        String str = null;
        if (c == null) {
            c = null;
        }
        w f;
        if (c.c.b.a.b.a.e.f.e(cVar)) {
            String c2 = cVar.f.c("Transfer-Encoding");
            if (c2 != null) {
                str = c2;
            }
            String str2 = "state: ";
            Logger logger;
            StringBuilder g;
            if ("chunked".equalsIgnoreCase(str)) {
                c.c.b.a.b.w wVar = cVar.a.a;
                if (this.e == 4) {
                    this.e = 5;
                    d dVar = new d(wVar);
                    logger = o.a;
                    return new h(c, -1, new r(dVar));
                }
                g = c.b.a.a.a.g(str2);
                g.append(this.e);
                throw new IllegalStateException(g.toString());
            }
            long b = c.c.b.a.b.a.e.f.b(cVar);
            if (b != -1) {
                f = f(b);
                logger = o.a;
                return new h(c, b, new r(f));
            } else if (this.e == 4) {
                c.c.b.a.b.a.c.g gVar = this.b;
                if (gVar != null) {
                    this.e = 5;
                    gVar.i();
                    g gVar2 = new g(this);
                    logger = o.a;
                    return new h(c, -1, new r(gVar2));
                }
                throw new IllegalStateException("streamAllocation == null");
            } else {
                g = c.b.a.a.a.g(str2);
                g.append(this.e);
                throw new IllegalStateException(g.toString());
            }
        }
        f = f(0);
        Logger logger2 = o.a;
        return new h(c, 0, new r(f));
    }

    public v c(c0 c0Var, long j) {
        String str = "state: ";
        StringBuilder g;
        if ("chunked".equalsIgnoreCase(c0Var.c.c("Transfer-Encoding"))) {
            if (this.e == 1) {
                this.e = 2;
                return new c();
            }
            g = c.b.a.a.a.g(str);
            g.append(this.e);
            throw new IllegalStateException(g.toString());
        } else if (j == -1) {
            throw new IllegalStateException("Cannot stream a request body without chunked encoding or a known content length!");
        } else if (this.e == 1) {
            this.e = 2;
            return new e(j);
        } else {
            g = c.b.a.a.a.g(str);
            g.append(this.e);
            throw new IllegalStateException(g.toString());
        }
    }

    public void d(k kVar) {
        x xVar = kVar.e;
        kVar.e = x.d;
        xVar.f();
        xVar.e();
    }

    public void e(c.c.b.a.b.v vVar, String str) throws IOException {
        if (this.e == 0) {
            String str2 = "\r\n";
            this.d.b(str).b(str2);
            int a = vVar.a();
            for (int i = 0; i < a; i++) {
                this.d.b(vVar.b(i)).b(": ").b(vVar.d(i)).b(str2);
            }
            this.d.b(str2);
            this.e = 1;
            return;
        }
        StringBuilder g = c.b.a.a.a.g("state: ");
        g.append(this.e);
        throw new IllegalStateException(g.toString());
    }

    public w f(long j) throws IOException {
        if (this.e == 4) {
            this.e = 5;
            return new f(this, j);
        }
        StringBuilder g = c.b.a.a.a.g("state: ");
        g.append(this.e);
        throw new IllegalStateException(g.toString());
    }

    public c.c.b.a.b.v g() throws IOException {
        c.c.b.a.b.v.a aVar = new c.c.b.a.b.v.a();
        while (true) {
            String h = h();
            if (h.length() == 0) {
                return new c.c.b.a.b.v(aVar);
            }
            Objects.requireNonNull((c.c.b.a.b.z.a) c.c.b.a.b.a.b.a);
            String str = ":";
            int indexOf = h.indexOf(str, 1);
            if (indexOf != -1) {
                aVar.b(h.substring(0, indexOf), h.substring(indexOf + 1));
            } else {
                String str2 = "";
                if (h.startsWith(str)) {
                    h = h.substring(1);
                    aVar.a.add(str2);
                    aVar.a.add(h.trim());
                } else {
                    aVar.a.add(str2);
                    aVar.a.add(h.trim());
                }
            }
        }
    }

    public final String h() throws IOException {
        String z = this.c.z(this.f);
        this.f -= (long) z.length();
        return z;
    }

    public void a() throws IOException {
        this.d.flush();
    }

    public c.c.b.a.b.c.a a(boolean z) throws IOException {
        int i = this.e;
        if (i == 1 || i == 3) {
            try {
                j a = j.a(h());
                c.c.b.a.b.c.a aVar = new c.c.b.a.b.c.a();
                aVar.b = a.a;
                aVar.c = a.b;
                aVar.d = a.c;
                aVar.a(g());
                if (z && a.b == 100) {
                    return null;
                }
                this.e = 4;
                return aVar;
            } catch (EOFException e) {
                StringBuilder g = c.b.a.a.a.g("unexpected end of stream on ");
                g.append(this.b);
                IOException iOException = new IOException(g.toString());
                iOException.initCause(e);
                throw iOException;
            }
        }
        StringBuilder g2 = c.b.a.a.a.g("state: ");
        g2.append(this.e);
        throw new IllegalStateException(g2.toString());
    }

    public void b() throws IOException {
        this.d.flush();
    }
}
